package com.jingdianjichi.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.convert.SubjectCategoryDTOConverter;
import com.jingdianjichi.subject.application.convert.SubjectLabelDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectCategoryDTO;
import com.jingdianjichi.subject.application.dto.SubjectLabelDTO;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.domain.service.SubjectLabelDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 标签的controller
 */
@RestController
@RequestMapping("/subject/label")
@Slf4j
public class SubjectLabelController {

    //注入领对象
    @Resource
    private SubjectLabelDomainService subjectLabelDomainService;

    /**
     * 添加标签
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLabelDTO subjectLabelDTO){
        //左逻辑判断
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectLabelController.add.dto:{}", JSON.toJSONString(subjectLabelDTO));
            }

            //参数校验,名字不能是空的
            Preconditions.checkNotNull(subjectLabelDTO.getLabelName(),"标签不能是空的");

            //转换对象
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDToLabelBO(subjectLabelDTO);

            boolean result = subjectLabelDomainService.add(subjectLabelBO);

            return Result.ok(result);
        }catch (Exception e){
            log.error("SubjectLabelController.add.error:{}",e.getMessage(),e);

            return Result.fail("新增标签失败");
        }
    }

    /**
     * 更新标签
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectLabelController.update.dto:{}",JSON.toJSONString(subjectLabelDTO));
            }

            //参数判断,id不能是空的
            Preconditions.checkNotNull(subjectLabelDTO.getId(),"id不能是空的");

            //转换
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDToLabelBO(subjectLabelDTO);

            //更新分类
            Boolean result= subjectLabelDomainService.update(subjectLabelBO);

            //返回数据
            return Result.ok(result);
        }catch (Exception e){
            log.error("SubjectLabelController.update.error:{}",e.getMessage(),e);
            return Result.fail("更新标签失败");
        }
    }


    /**
     * 删除标签
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectLabelController.delete.dto:{}",JSON.toJSONString(subjectLabelDTO));
            }

            //参数判断,id不能是空的
            Preconditions.checkNotNull(subjectLabelDTO.getId(),"id不能是空的");

            //转换
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDToLabelBO(subjectLabelDTO);

            //更新分类
            Boolean result= subjectLabelDomainService.delete(subjectLabelBO);

            //返回数据
            return Result.ok(result);
        }catch (Exception e){
            log.error("SubjectLabelController.delete.error:{}",e.getMessage(),e);
            return Result.fail("删除标签失败");
        }
    }


    /**
     *查询分类下标签
     */
    @PostMapping("/queryLabelByCategoryId")
    public Result<List<SubjectLabelDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectLabelController.queryLabelByCategoryId.dto:{}",JSON.toJSONString(subjectLabelDTO));
            }

            //参数判断,id不能是空的
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(),"分类id不能是空的");

            //转换
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDToLabelBO(subjectLabelDTO);

            //查询
            List<SubjectLabelBO> boList= subjectLabelDomainService.queryLabelByCategoryId(subjectLabelBO);

            List<SubjectLabelDTO> dtoList = SubjectLabelDTOConverter.INSTANCE.convertBOLabelDTOList(boList);

            //返回数据
            return Result.ok(dtoList);
        }catch (Exception e){
            log.error("SubjectLabelController.queryLabelByCategoryId.error:{}",e.getMessage(),e);
            return Result.fail("查询分类下标签失败");
        }
    }
}
